package com.synnapps.example.carouselview;

import com.synnapps.carouselview.CarouselView;
import com.synnapps.carouselview.ImageClickListener;
import com.synnapps.carouselview.ImageListener;
import com.synnapps.carouselview.ViewListener;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;

import ohos.agp.components.*;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.ToastDialog;
import ohos.agp.window.service.Window;

/**
 * 首页
 */
public class SampleCarouselViewAbility extends Ability {
    int[] sampleImages = {ResourceTable.Media_image_1, ResourceTable.Media_image_2,
            ResourceTable.Media_image_3, ResourceTable.Media_image_4, ResourceTable.Media_image_5};
    String[] sampleTitles = {"Orange", "Grapes", "Strawberry", "Cherry", "Apricot"};
    CarouselView carouselView;
    CarouselView customCarouselView;
    Button pauseButton;

    @Override
    public void onStart(Intent intent) {
        super.setUIContent(ResourceTable.Layout_ability_sample_carousel_view);
        Window window = getWindow();
        window.setStatusBarColor(Color.getIntColor("#c0392b"));
        window.setStatusBarVisibility(Component.VISIBLE);
        carouselView = (CarouselView) findComponentById(ResourceTable.Id_carouselView);
        customCarouselView = (CarouselView) findComponentById(ResourceTable.Id_customCarouselView);
        pauseButton = (Button) findComponentById(ResourceTable.Id_pauseButton);
        carouselView.setImageListener(imageListener);
        customCarouselView.setViewListener(viewListener);
        carouselView.setPageCount(sampleImages.length);
        customCarouselView.setPageCount(sampleImages.length);
        pauseButton.setClickedListener(pauseOnClickListener);
        carouselView.setImageClickListener(new ImageClickListener() {
            @Override
            public void onClick(int position) {
                new ToastDialog(getContext()).setText("Clicked item: " + position).show();
            }
        });
    }

    ImageListener imageListener = new ImageListener() {
        @Override
        public void setImageForPosition(int position, Image imageView) {
            imageView.setPixelMap(sampleImages[3]);
        }
    };

    ViewListener viewListener = new ViewListener() {
        @Override
        public Component setViewForPosition(int position) {
            Component customView = LayoutScatter.getInstance(getContext())
                    .parse(ResourceTable.Layout_view_custom, null, true);
            Text labelTextView = (Text) customView.findComponentById(ResourceTable.Id_labelTextView);
            Image fruitImageView = (Image) customView.findComponentById(ResourceTable.Id_fruitImageView);
            fruitImageView.setPixelMap(sampleImages[position]);
            labelTextView.setText(sampleTitles[position]);
            carouselView.setIndicatorGravity(LayoutAlignment.HORIZONTAL_CENTER | LayoutAlignment.TOP);
            return customView;
        }
    };

    Component.ClickedListener pauseOnClickListener = new Component.ClickedListener() {
        @Override
        public void onClick(Component component) {
            carouselView.pauseCarousel();
            customCarouselView.reSetSlideInterval(0);
        }
    };
}
